package com.be.beadmin.product.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.product.dto.input.WrokingHoursOverViewDto;
import com.be.beadmin.product.dto.output.WorkingHoursOverViewExcelEx;
import com.be.beadmin.product.dto.output.WrokingHoursOverViewEx;
import com.be.beadmin.product.service.WrokingHoursOverViewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author zjm
* @date 2023-04-24
**/
@Api(tags = "生产工序计时")
@RestController
public class WrokingHoursOverViewController {

    @Autowired
    private  WrokingHoursOverViewService wrokingHoursOverViewService;


    @PostMapping(value = "/product/getWrokingHoursOverViewByPage")
    public ResultModel getWrokingHoursOverViewByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(wrokingHoursOverViewService.getWrokingHoursOverViewByPage(pageEntity)) ;
    }

    @PostMapping("/product/addWrokingHoursOverView")
    public ResultModel addWrokingHoursOverView(@RequestBody WrokingHoursOverViewDto wrokingHoursOverViewDto){
        wrokingHoursOverViewService.addWrokingHoursOverView(wrokingHoursOverViewDto);
        return ResultModel.success(ResultModel.ADD);
    }
    @ApiOperation(value = "获取当前计时总量和最后一次计时开始时间")
    @PostMapping("/product/getSumTimingAndCurStartTime")
    public ResultModel getSumTimingAndCurStartTime(HttpServletRequest request, @RequestParam String processId){

        return ResultModel.success(wrokingHoursOverViewService.getSumTimingAndCurStartTime(request,processId));
    }

    @PostMapping("/product/delWrokingHoursOverView")
    public ResultModel delWrokingHoursOverView(@RequestBody WrokingHoursOverViewDto wrokingHoursOverViewDto){
        wrokingHoursOverViewService.delWrokingHoursOverView(wrokingHoursOverViewDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }
    @PostMapping("/product/updateWrokingHoursOverView")
    public ResultModel updateWrokingHoursOverView(@RequestBody WrokingHoursOverViewDto wrokingHoursOverViewDto){
        wrokingHoursOverViewService.updateWrokingHoursOverView(wrokingHoursOverViewDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/product/exportWrokingHoursOverView")
    public void exportWrokingHoursOverView(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<WrokingHoursOverViewEx> wrokingHoursOverViewPage = wrokingHoursOverViewService.getWrokingHoursOverViewByPage(pageEntity);
        List<WrokingHoursOverViewEx> wrokingHoursOverViewExes = wrokingHoursOverViewPage.getContent();
        List<WorkingHoursOverViewExcelEx> workingHoursOverViewExcelExes=wrokingHoursOverViewExes.stream().map(item->{
            WorkingHoursOverViewExcelEx excel=new WorkingHoursOverViewExcelEx();
            BeanUtils.copyProperties(item,excel);
            return excel;
        }).collect(Collectors.toList());
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("WrokingHoursOverView接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
        //EasyExcel.write(response.getOutputStream(), WorkingHoursOverViewExcelEx.class).sheet("sheet1").doWrite(workingHoursOverViewExcelExes);
        ExcelWriterBuilder ewb=EasyExcel.write(response.getOutputStream(), WorkingHoursOverViewExcelEx.class);
        ewb.excelType(ExcelTypeEnum.XLS);
        ewb.sheet("sheet1").doWrite(workingHoursOverViewExcelExes);
    }
}